<?php
namespace app\admin\controller;

use app\common\model\Administrator as M_User;
use app\common\validate\Administrator as V_User;
use bigDream\thinkJump\Jump;
use think\facade\Request;

/**
 * Class Index
 * @package app\admin\controller
 * @author jwj <jwjbjg@gmail.com>
 * @copyright jwj
 */
class Index
{
    /**
     * 后台首页
     * @return \think\response\View
     */
    public function index()
    {
        return view('', ['User' => app('Administrator')]);
    }

    /**
     * 个人账户
     * @return \think\response\View
     */
    public function profile()
    {
        $result = app('Administrator');

        if (!Request::isAjax()) {
            return view('', [
                'result' => $result,
            ]);
        }

        $validate = (new V_User())->scene('profile');
        $data = Request::only($validate->getSceneField());
        $data['id'] = $result['id'];
        if (!$validate->check($data)) {
            Jump::error($validate->getError());
        }

        if (empty($data['password'])) unset($data['password']);

        M_User::startTrans();
        try {
            $result->save($data);

            M_User::commit();
        } catch (\Exception $e) {
            M_User::rollback();

            Jump::error('发生错误:' . $e->getMessage());
        }

        Jump::success('修改成功');
    }
}